package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.entity.*;
import com.mall.httpResult.HttpResult;
import com.mall.httpResult.ResultCodeEnum;
import com.mall.mapper.*;
import com.mall.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fire
 * @since 2020-04-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrdersMapper orderMapper;

    @Autowired
    private OrderproductMapper orderproductMapper;

    @Autowired
    private OrderreturnMapper orderreturnMapper;

    @Override
    public String findPasswordByPhone(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);
        queryWrapper.select("password");
        List<User> users = userMapper.selectList(queryWrapper);
        return users.get(0).getPassword();
    }

    @Override
    public int saveUser(User user) {
       return userMapper.insert(user);
    }

    @Override
    public int selectIdByPhone(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",phone);
        queryWrapper.select("id");
        List<User> users = userMapper.selectList(queryWrapper);
        return users.get(0).getId();
    }

    @Override
    public List<Orders> listOrdersByUserIdAndStatus(int userId, String status , int pageNo, int pageSize) {

        Page<Orders> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        if(!"-1".equals(status)){
            wrapper.eq("status", status);
        }
        orderMapper.selectPage(page, wrapper);
        List<Orders> orderList = page.getRecords();

        User user = userMapper.selectById(userId);

        for (Orders order : orderList) {
            
            QueryWrapper<Orderproduct> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("order_id", order.getId());
            List<Orderproduct> orderproductList = orderproductMapper.selectList(wrapper2);
            
            for (Orderproduct orderproduct : orderproductList) {
                Product product = productMapper.selectById(orderproduct.getProductId());
                product.setCount(orderproduct.getCount());
                order.getProductList().add(product);
            }
            order.setUser(user);
        }

        return orderList;
    }

    @Override
    public List<Orderproduct> listOrderProductByUserId(int userId , String status) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        if(!"-1".equals(status)){
            wrapper.eq("status",status);
        }
        orderMapper.selectList(wrapper);
        List<Orders> ordersList = orderMapper.selectList(wrapper);
        List<Orderproduct> orderproductList = new ArrayList<>();
        for(Orders order : ordersList){
            QueryWrapper<Orderproduct> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("order_id",order.getId());
           orderproductList.addAll(orderproductMapper.selectList(wrapper1));
        }
        for(Orderproduct orderproduct : orderproductList){
            orderproduct.setProduct(productMapper.selectById(orderproduct.getProductId()));
        }
        return orderproductList;
    }

    @Transactional
    @Override
    public boolean returnOrder(int userId , int orderId , String description){
        Orders order = orderMapper.selectById(orderId);
        if (order == null || order.getUserId() != userId){
            return false;
        }
        UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status","3");
        updateWrapper.eq("id",orderId);
        orderMapper.update(order,updateWrapper);
        QueryWrapper<Orderreturn> orderreturnQueryWrapper = new QueryWrapper<>();
        orderreturnQueryWrapper.eq("order_id",orderId);
        if(orderreturnMapper.selectOne(orderreturnQueryWrapper) != null){
            return true;
        }
        Orderreturn orderreturn = new Orderreturn();
        orderreturn.setOrderId(orderId);
        orderreturn.setDescription(description);
        orderreturn.setStatus("0");
        orderreturnMapper.insert(orderreturn);
        return true;
    }

    @Override
    public HttpResult getOrderReturnStatus(int userId, int orderId) {
        Orders order = orderMapper.selectById(orderId);
        if (order == null || order.getUserId() != userId){
            return HttpResult.failure(ResultCodeEnum.ORDER_NOTFOUND);
        }
        if (!order.getStatus().equals("3")){
            return HttpResult.failure(ResultCodeEnum.ORDER_NOTRETURN);
        }
        QueryWrapper<Orderreturn> orderreturnQueryWrapper = new QueryWrapper<>();
        orderreturnQueryWrapper.eq("order_id",orderId);
        Orderreturn orderreturn = orderreturnMapper.selectOne(orderreturnQueryWrapper);
        if (orderreturn != null){
            return HttpResult.success(orderreturn.getStatus());
        }
        return HttpResult.failure(ResultCodeEnum.ORDER_NOTRETURN);
    }

    @Transactional
    @Override
    public HttpResult evaluate(Orderproduct orderproduct) {
        QueryWrapper<Orderproduct> orderproductQueryWrapper = new QueryWrapper<>();
        orderproductQueryWrapper.eq("order_id",orderproduct.getOrderId());
        orderproductQueryWrapper.eq("product_id",orderproduct.getProductId());
        Orderproduct orderproductOld = orderproductMapper.selectOne(orderproductQueryWrapper);
        if(orderproductOld != null && orderproductOld.getStart()!=null){
            return HttpResult.failure(ResultCodeEnum.REPEATE_EVALUATE);
        }
        UpdateWrapper<Orderproduct> orderproductUpdateWrapper = new UpdateWrapper<>();
        orderproductUpdateWrapper.set("evaluate",orderproduct.getEvaluate());
        orderproductUpdateWrapper.set("start",orderproduct.getStart());
        orderproductUpdateWrapper.eq("order_id",orderproduct.getOrderId());
        orderproductUpdateWrapper.eq("product_id",orderproduct.getProductId());
        orderproductMapper.update(orderproduct,orderproductUpdateWrapper);
        return HttpResult.success();
    }

}
